package com.zam.web.sysController;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zam.comment.Constants;
import com.zam.comment.Result;
import com.zam.mapper.SysFileMapper;
import com.zam.pojo.SysFile;
import com.zam.service.ISysFileService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.zam.comment.Constants.CODE_600;

/******************************
 controller层
 ******************************/
@RestController
@RequestMapping("/file")
public class SysFileController {
    @Autowired
    private ISysFileService sysFileService;

    @Autowired
    private SysFileMapper sysFileMapper;

    @Value("${file.upload.path}")
    private String fileUploadPath;

    // 上传文件
    @PostMapping("/upload")
    public Result upload(@RequestParam MultipartFile file, HttpServletRequest request) throws IOException {
        Map<String, Object> map = sysFileService.saveFile(file, request);
        if (map.get("code")==CODE_600){
            return Result.error(CODE_600, "文件已经存在");
        }
        return Result.success();

    }

    //下载文件
    @GetMapping("/download/{FileUUID}")
    public Result download(@PathVariable String FileUUID, HttpServletResponse response) throws IOException {
        File originalFile = new File(fileUploadPath + FileUUID); // 下载路径
        ServletOutputStream os = response.getOutputStream(); // 输出流
        os.write(FileUtil.readBytes(originalFile)); // 写出数据
        os.flush();
        os.close();
        return Result.success();
    }


    /*
    添加数据
    */
    @PostMapping("/update")
    public Result update(@RequestBody SysFile sysFile) {
        sysFileService.saveOrUpdate(sysFile);
        return Result.success();
    }

    /*
    删除数据
    */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(sysFileMapper.deleteById(id));
    }

    /*
    删除多个数据
    */
    @DeleteMapping("/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        sysFileService.removeByIds(ids);
        return Result.success();
    }

    /*
     查看全部
    */
    @GetMapping
    public List<SysFile> findAll() {
        return sysFileService.list();
    }

    /*
    根据id查看
    */
    @GetMapping("/{id}")
    public SysFile findOne(@PathVariable Integer id) {
        return sysFileService.getById(id);
    }

    /*
    分页查询
     */
    @GetMapping("/page")
    public Result limitSelect(int num, int pagesize, SysFile sysFile, Integer mixSize) {
        IPage<SysFile> userPage = new Page<>(num, pagesize);
        LambdaQueryWrapper<SysFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(sysFile.getName())) {
            lambdaQueryWrapper.like(SysFile::getName, sysFile.getName());
        }
        if (StrUtil.isNotEmpty(sysFile.getType())) {
            lambdaQueryWrapper.like(SysFile::getType, sysFile.getType());
        }
        if (sysFile.getSize() != null && sysFile.getSize() > 0) {
            lambdaQueryWrapper.ge(SysFile::getSize, sysFile.getSize());
        }
        if (mixSize != null && mixSize > 0) {
            lambdaQueryWrapper.le(SysFile::getSize, mixSize);
        }
        lambdaQueryWrapper.orderByDesc(SysFile::getId);
        return Result.success(sysFileMapper.selectPage(userPage, lambdaQueryWrapper));
    }
}

